/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  SRVGetGPSData.m - iPhoneSRV console for Surveyor SRV-1 and SVS 
 *
 *  Created by Nick Kitchener.  Copyright (C) 2009  Surveyor Corporation 
 *            and Nick Kitchener.  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details (www.gnu.org/licenses)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#import "SRVGetGPSData.h"

/* #define METERS_PER_DEGREE 111319.5
 * #define FEET_PER_DEGREE  365221
 * #define MILES_PER_DEGREE 69.17   // equatorial  (68.939 polar)
 
 * $GPGGA,173415.400,3514.5974,N,12037.2028,W,1,8,1.18,84.6,M,-30.8,M,,*50
 * $GPGGA,hhmmss.sss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,g.g,z,t.t,iii*CC
 * where:
 *    GPGGA        : GPS fixed data identifier
 *    hhmmss.ss    : Coordinated Universal Time (UTC), also known as GMT
 *    ddmm.mmmm,n  : Latitude in degrees, minutes and cardinal sign
 *    dddmm.mmmm,e : Longitude in degrees, minutes and cardinal sign
 *    q            : Quality of fix.  1 = there is a fix
 *    ss           : Number of satellites used
 *    y.y          : Horizontal precision
 *    a.a,M        : GPS antenna altitude in meters
 *    g.g,M        : geoidal separation in meters
 *    t.t          : age of the differential correction data
 *    iiii         : Differential station's ID
 *    *CC          : Checksum 
 */

@implementation SRVGetGPSData

-(id)init {
	if( self = [super init] ) {
		request = [[SRVStreamingRequestBuffer alloc] init];
		response = [[SRVStreamingSelfTerminatedStringResponseBuffer alloc] init];
		
		NSString *reqString = [NSString stringWithUTF8String:"g"];
		[request setRequestString:reqString];
	}
	return self;
}

-(void) dealloc {
	[request release];
	[response release];
	request = nil;
	response = nil;
	[super dealloc];
}

-(void)haveBytes:(CFReadStreamRef)inputStream {
	[response haveBytes:inputStream];
	if([response isComplete]) {
		complete = YES;
	}
}

-(void)haveSpace:(CFWriteStreamRef)outputStream {
	[request haveSpace:outputStream];
}

-(void)parseRawGPSOutput {
	NSScanner *responseScanner = [[NSScanner alloc]initWithString:[response getResponse]];

	// $GPGGA,hhmmss.sss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,g.g,z,t.t,iii*CC
	
	[responseScanner scanUpToString:@"," intoString:NULL];
	
	NSString *timestamp = nil;
	[responseScanner scanUpToString:@"," intoString:&timestamp];	
	[responseScanner scanString:@"," intoString:NULL];
	
	NSString *latitude = nil;
	[responseScanner scanUpToString:@"," intoString:&latitude];	
	[responseScanner scanString:@"," intoString:NULL];

	NSString *latitudeCardinal = nil;
	[responseScanner scanUpToString:@"," intoString:&latitudeCardinal];
	[responseScanner scanString:@"," intoString:NULL];

	NSString *longitude = nil;	
	[responseScanner scanUpToString:@"," intoString:&longitude];
	[responseScanner scanString:@"," intoString:NULL];

	NSString *longitudeCardinal = nil;
	[responseScanner scanUpToString:@"," intoString:&longitudeCardinal];	
	[responseScanner scanString:@"," intoString:NULL];

	NSString *quality = nil;	
	[responseScanner scanUpToString:@"," intoString:&quality];	
	[responseScanner scanString:@"," intoString:NULL];
	
	NSString *numberOfSats = nil;	
	[responseScanner scanUpToString:@"," intoString:&numberOfSats];	
	[responseScanner scanString:@"," intoString:NULL];
	
	NSString *horizontalAccuracy = nil;	
	[responseScanner scanUpToString:@"," intoString:&horizontalAccuracy];	
	[responseScanner scanString:@"," intoString:NULL];
	
	NSString *altitude = nil;	
	[responseScanner scanUpToString:@"," intoString:&altitude];	
	[responseScanner scanString:@"M," intoString:NULL];

	NSString *geoSeparation = nil;	
	[responseScanner scanUpToString:@"," intoString:&geoSeparation];	
	[responseScanner scanString:@"M," intoString:NULL];
	
	NSString *age = nil;	
	[responseScanner scanUpToString:@"," intoString:&age];	
	[responseScanner scanString:@"," intoString:NULL];

	NSString *diffStationId = nil;	
	[responseScanner scanUpToString:@"*" intoString:&diffStationId];	
	[responseScanner scanString:@"*" intoString:NULL];
	
	NSString *crc = nil;	
	[responseScanner scanUpToString:@"\n" intoString:&crc];	
	
	
	CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(<#CLLocationDegrees latitude#>, <#CLLocationDegrees longitude#>)
	CLLocationDistance altitude = 
	CLLocationAccuracy hAccuraccy =
	CLLocationAccuracy vAccuracy =
	
	location = [[CLLocation alloc]
	initWithCoordinate:(CLLocationCoordinate2D)coordinate
	altitude:(CLLocationDistance)altitude
	horizontalAccuracy:(CLLocationAccuracy)hAccuracy
	verticalAccuracy:(CLLocationAccuracy)vAccuracy
	timestamp:(NSDate *)timestamp	
}

@end
